//
//  QJHomeVCClassVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJHomeVCClassVC.h"
//数据为空
#import "UIScrollView+EmptyDataSet.h"
#import "QJSearchVCCell.h"
//商品详情
#import "QJShopDetailVC.h"

@interface QJHomeVCClassVC () <UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource,UISearchBarDelegate>
@property (nonatomic, strong) UITableView *classTableView;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSString *selectPaiXu;
@property (nonatomic, strong) NSString *searchStr;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,copy)NSString *searchString;
@end

@implementation QJHomeVCClassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH - 100, 30)];//allocate titleView
   
    [titleView setBackgroundColor:[UIColor clearColor]];
    
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.frame = CGRectMake(0, 0, UISCREEN_WIDTH - 100, 30);
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.delegate = self;
    searchBar.placeholder = @"请输入搜索内容";
    searchBar.barStyle = UISearchBarStyleDefault;
    [titleView addSubview:searchBar];
    self.searchBar = searchBar;
    self.navigationItem.titleView = titleView;
    
    //设置背景图是为了去掉上下黑线
    searchBar.backgroundImage = [[UIImage alloc] init];
    // 设置SearchBar的颜色主题为白色
    searchBar.barTintColor = [UIColor whiteColor];
    searchBar.backgroundColor = [UIColor whiteColor];
    [self addRightBtn];
    [self setupClassUIAction];
    [self defaultUIPrepareAction];
     [self classDataRequestAction];
}

- (void)addRightBtn {
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    [rightBarItem setImage:[UIImage imageNamed:@"HSearch"]];
    [rightBarItem setTintColor:[UIColor whiteColor]];
//    [rightBarItem setTintColor:[UIColor lightGrayColor]];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)onClickedOKbtn {
    
    self.pageNumber = 1;
    [self classDataRequestAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
#pragma mark -- 默认配置
- (void)defaultUIPrepareAction{
    self.searchStr = @"";
    self.selectPaiXu = @"click_count";
    self.pageNumber = 1;
    self.firstBtn.selected = YES;
}
#pragma mark -- 界面布局
- (void)setupClassUIAction{
    //修改placeholder颜色
//    self.searchTF = [UITextField changeTextField:self.searchTF PlaceholderColorWith:HexRGB(0x666666) WithFont:15];
    
    self.classTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - 100) style:UITableViewStylePlain];
    [self.tableView addSubview:self.classTableView];
    self.classTableView.delegate = self;
    self.classTableView.dataSource = self;
    self.classTableView.emptyDataSetDelegate = self;
    self.classTableView.emptyDataSetSource = self;
    self.classTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.classTableView registerNib:[UINib nibWithNibName:@"QJSearchVCCell" bundle:nil] forCellReuseIdentifier:@"cellID"];
    self.classTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HRefresh)];
    self.classTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(FRefresh)];
    self.pageNumber = 1;
}
#pragma mark -- 数据刷新
- (void)HRefresh{
    self.pageNumber = 1;
    [self classDataRequestAction];
}
- (void)FRefresh{
    self.pageNumber++;
    [self classDataRequestAction];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QJSearchVCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SearchMdoel *model = self.dataSourceArray[indexPath.row];
    [cell cellUpdateWithModel:model];
    return cell;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchMdoel *model = self.dataSourceArray[indexPath.row];
    QJShopDetailVC *VC = [[QJShopDetailVC alloc]init];
    VC.goodsID = [NSString stringWithoutNilFormart:model.goods_id];
    KPushViewControllerAction(VC);
}
#pragma mark -- DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"noDataImage"];
}
#pragma mark -- DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
- (IBAction)classVCBtnClickAction:(UIButton *)sender {
    NSInteger tag = sender.tag;
    self.firstBtn.selected = NO;
    self.secondBtn.selected = NO;
    switch (tag) {
       
        case 201:
        {
            self.firstBtn.selected = YES;
            self.pageNumber = 1;
            self.selectPaiXu = @"click_count";
            [self classDataRequestAction];
        }
            break;
        case 202:
        {
            self.secondBtn.selected = YES;
            self.pageNumber = 1;
            self.selectPaiXu = @"shop_price";
            [self classDataRequestAction];
        }
            break;
            
        default:
        break;
    }
}

#pragma mark -- 数据请求方法
- (void)classDataRequestAction{
   
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if ([self.type isEqualToString:@"1"]) {
        
  
        [dict setObject:self.classID forKey:@"id"];
    }else{
        
         [dict setObject:self.classTwoID forKey:@"id"];
    }
   
    [dict setObject:self.searchString forKey:@"keywords"];
    [dict setObject:self.selectPaiXu forKey:@"sort"];

    [dict setObject:[NSString stringWithFormat:@"%ld",(long)self.pageNumber] forKey:@"p"];


    [WWNetRequest fetchGoodsListWithParameters:dict success:^(id response) {
        
        NSLog(@"商品搜索 response = %@", response);

        if (self.pageNumber == 1) {
            [self.dataSourceArray removeAllObjects];
        }
        id data = response[@"data"][@"goods_list"];
        
        if ([data isKindOfClass:[NSArray class]]) {
            
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                SearchMdoel *model = [[SearchMdoel alloc]initWithDictionary:obj error:nil];
                [self.dataSourceArray addObject:model];
                
            }];
            
            [self.classTableView reloadData];
            
        }else{
            
            [self.dataSourceArray removeAllObjects];
        }
       
    } failure:^(NSDictionary *error) {
        NSLog(@"商品搜索 error = %@", error);
    }];
    [self.classTableView.mj_header endRefreshing];
    [self.classTableView.mj_footer endRefreshing];
}
#pragma mark -- 懒加载
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.searchBar resignFirstResponder];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    //已经开始编辑
}// called when text starts editing

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"收键盘监听");
   
}// called when text ends editing


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    //searchText是searchBar上的文字 每次输入或删除都都会打印全部
    self.pageNumber = 1;
   _searchString = searchText;
}

//点击键盘上搜索时的相应事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
    self.pageNumber = 1;
    [self classDataRequestAction];
}// called when keyboard search button pressed

@end
